package com.waterworld.watch.chat.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.waterworld.watch.R;
import com.waterworld.watch.common.bean.GroupMemberBean;
import com.waterworld.watch.common.customview.CircleImageView;
import com.waterworld.watch.common.net.UrlContants;
import com.waterworld.watch.common.util.PhotoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class EditGroupAdapter extends RecyclerView.Adapter<EditGroupAdapter.MyHolder>{

    private Context mContext;
    private List<GroupMemberBean> mList;

    public EditGroupAdapter(Context context,List<GroupMemberBean> list) {
        mContext = context;
        mList = list;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_edit_group,viewGroup,false);
        view.getLayoutParams().height = viewGroup.getHeight() / 2;
        view.getLayoutParams().width = viewGroup.getWidth() / 4;
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder myHolder, int i) {
        if (mList.get(i).getHead() == null) {
            Glide.with(mContext).load(R.drawable.headimage_default).into(myHolder.iv_avatar);
        } else if (mList.get(i).getHead().equals("ic_group_add")) {
            Glide.with(mContext).load(R.drawable.ic_group_add).into(myHolder.iv_avatar);
        } else if (mList.get(i).getHead().equals("ic_group_delete")) {
            Glide.with(mContext).load(R.drawable.ic_group_delete).into(myHolder.iv_avatar);
        } else if (mList.get(i).getHead().startsWith("sys")) {
            if (mList.get(i).getMemberType() == 1) {
                Glide.with(mContext).load(PhotoUtils.getResource(mList.get(i).getHead().substring(0, 13))).into(myHolder.iv_avatar);
            } else if (mList.get(i).getMemberType() == 2) {
                Glide.with(mContext).load(PhotoUtils.getResource(mList.get(i).getHead().substring(0, 12))).into(myHolder.iv_avatar);
            }
        } else {
            Glide.with(mContext).load(UrlContants.FILE_URL + "resources/watch/" + mList.get(i).getHead()).into(myHolder.iv_avatar);
        }

        if (mList.get(i).getMemberType() != null) {
            if (mList.get(i).getMemberType() == 1) {
                Glide.with(mContext).load(R.drawable.ic_app_user).into(myHolder.iv_member_type);
            } else if (mList.get(i).getMemberType() == 2) {
                Glide.with(mContext).load(R.drawable.ic_watch_user).into(myHolder.iv_member_type);
            }
        }
        myHolder.tv_member_name.setText(mList.get(i).getMemberName());

        myHolder.rl_group_member.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onItemClick(v,i);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rl_group_member)
        RelativeLayout rl_group_member;

        @BindView(R.id.iv_avatar)
        CircleImageView iv_avatar;

        @BindView(R.id.iv_member_type)
        ImageView iv_member_type;

        @BindView(R.id.tv_member_name)
        TextView tv_member_name;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface Listener {
        void onItemClick(View view,int position);
    }

    public Listener listener;

    public void setOnItemClickListener(Listener listener) {
        this.listener = listener;
    }
}
